package controllers.commands
{
	import com.google.maps.InfoWindowOptions;
	import com.google.maps.LatLng;
	import com.google.maps.Map;
	import com.google.maps.MapMouseEvent;
	import com.google.maps.overlays.Marker;
	import com.google.maps.overlays.MarkerOptions;
	import com.google.maps.styles.FillStyle;
	import com.google.maps.styles.StrokeStyle;
	
	import controllers.commands.base.ICommand;
	import controllers.managers.LocationQueryManager;
	
	import flash.events.Event;
	import flash.geom.Point;
	import flash.text.StyleSheet;
	import flash.text.TextFormat;
	
	import model.context.TrackerContext;
	import model.registry.ColorTable;
	
	import mx.messaging.AbstractConsumer;
	
	import utils.DeviceParser;
	
	import view.MapCanvas;
	
	public class DrawMarkerCommand implements ICommand
	{
		private var container:MapCanvas;
		private var context:TrackerContext;
		private var toolTipStr : String = "";
		private var locationManager:LocationQueryManager = null;
		private var map:Map;
		private var currentPositionData:Object;
		
		public function DrawMarkerCommand(view:MapCanvas)
		{
			container = view;
		}
		
		public function setContext(context:Object):void
		{
			this.context = context;
			this.map = this.context.mapCanvas.map; 
			currentPositionData = this.context.data;
			locationManager = new LocationQueryManager();
			locationManager.addEventListener(LocationQueryManager.EVENT_UPDATE, onLocationUpdateHandler);
		}
		
		/**
		 * 更新地址信息
		 **/ 
		private function onLocationUpdateHandler(evt:Event):void
		{
			if(currentPositionData != null)
			{
				var addresText : String = LocationQueryManager(evt.currentTarget).formattedAddress;
				var endLatLng:LatLng = new LatLng(currentPositionData.latitude,currentPositionData.longitude);
				
				var infoList : Array = currentPositionData.status_text;
				var infoParser : DeviceParser = new DeviceParser(currentPositionData);
				//准备备注信息
				toolTipStr  = "";
				toolTipStr += " 时间: "+Global.getTimeStrByTime(currentPositionData.dateline, true ) + " \n" ;
				toolTipStr += " 方向: "+infoParser.getDirection() + "\n" ;
				toolTipStr +=  " 时速: "+infoParser.getSpeed() + "KM/h\n" ;
				toolTipStr += " 经度: "+infoParser.getLng() + "\n" ;
				toolTipStr += " 纬度: "+infoParser.getLat() + "\n" ;
				toolTipStr += " 地址: " + addresText + " \n" ;
				
				var tempStr : String = "";
				if(infoList != null)
				{
					for (var ii:int=0;ii<infoList.length;ii++)
					{
						tempStr += infoList[ii] + " " ;
					}
					toolTipStr += " 状态: " + tempStr + "\n";
				}
				
				var opObj:Object = { 
					strokeStyle:new StrokeStyle({color: ColorTable.GREY}),
					fillStyle: new FillStyle({color: ColorTable.DARK_RED, alpha: 1}),
					radius: 12,
					hasShadow: true,
					label:"P"}
				
				//				if( marker == null )
				//				{
				var marker:Marker = new Marker(endLatLng, new MarkerOptions(opObj));   
				marker.addEventListener(MapMouseEvent.ROLL_OVER, onDisplayLocInfoByState);
				marker.addEventListener(MapMouseEvent.ROLL_OUT, function():void{ marker.closeInfoWindow(); });
				map.addOverlay(marker);
				//				}
				//				else
				//				{
				//					map.removeOverlay(marker);
				//				}
			}
		}
		
		public function execute():void
		{
			var eLatLng:LatLng = new LatLng(currentPositionData.latitude, 
											currentPositionData.longitude);
			createLocalInfoTip(eLatLng);
		}
		
		/**
		 * 创建位置详细信息提示
		 **/ 
		private function createLocalInfoTip(endLatLng:LatLng):void
		{
			
			locationManager.query( endLatLng.lat() + "," + endLatLng.lng());
			var opObj:Object = { 
				strokeStyle:new StrokeStyle({color: ColorTable.GREY}),
				fillStyle: new FillStyle({color: ColorTable.DARK_RED, alpha: 1}),
				radius: 12,
				hasShadow: true,
				label:"P"}
			//			if( marker == null )
			//			{
			var marker:Marker = new Marker(endLatLng, new MarkerOptions(opObj));   
			
			marker.addEventListener(MapMouseEvent.ROLL_OVER, onDisplayLocInfoByState);
			marker.addEventListener(MapMouseEvent.ROLL_OUT, function():void{ marker.closeInfoWindow(); });
			map.addOverlay(marker);
			//			}
			map.panTo(endLatLng);
		}
		
		/**
		 * 根据状态显示相应的颜色
		 **/
		private function onDisplayLocInfoByState(evt:MapMouseEvent):void
		{
			var marker:Marker = evt.currentTarget as Marker;
			if ( marker != null )
			{
				var contentStr:String = "";
				
				var contentStyle:StyleSheet = new StyleSheet();
				contentStyle.setStyle("color", "#00CC00"); 
				
				var contentFormat:TextFormat = new TextFormat();
				contentFormat.size = 12;
				contentFormat.color = ColorTable.BLACK;
				marker.openInfoWindow(new InfoWindowOptions({ content:toolTipStr,
					hasShadow : false,
					hasTail : false,
					width : 250,
					hasCloseButton : true,
					tailAlign : InfoWindowOptions.ALIGN_LEFT,
					pointOffset : new Point(130,130),
					contentFormat : contentFormat}));
			}				
		}
	}
}